import axios from 'axios'
//登陆
export function Login(dispatch,props,obj){
    return dispatch(()=>{
        axios.post('/worker/login', {
            phone: obj.username,
            password: obj.password
        }).then(res => {
            if(res.data.code === 1) {
              console.log( res.data.token)
              localStorage.setItem('token', res.data.token)
              props.history.push('/home')
            console.log('登录成功')
            } else{
                console.log('登录失败')
            }
        })
    })
    
}
//注册
export function Register(dispatch,obj,props){
    return dispatch(_ => {
        axios.post('/worker/register', {
            phone: obj.username,
            password: obj.password
        }).then(res => {
            if(res.data.code === 1) {
                props.history.push('/login')
                console.log('注册成功')
            } else{
                console.log('注册失败')
            }
        })
    })
}
//请求全部数据
export function getData(dispatch){
    return dispatch(_ => {
        axios({
          url: '/slides',
          methods: 'GET',
          headers: {
            "Authorization": localStorage.getItem('token')
          }
        }).then(res => {
          dispatch({
            type: 'SAVE_SLIDES',
            data: res.data.list
          })
        })
      })
}
//删除数据
export function DeleteUrl(dispatch,id){
    return dispatch( ()=>{
        axios({
          url: '/deleteUrl',
          method: 'POST',
          data: {
            id
          },
          headers: {
            "Authorization": localStorage.getItem('token')
          }
        }).then(res => {
          if(res.data.code===1){
            getData(dispatch)
          }
        })
      })
}